﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;

namespace XNA_Game_Framework
{
	public class ThemeManager
	{
		List<Theme.Theme> themes;

		public ThemeManager()
		{
			themes = new List<Theme.Theme>();
		}

		public Theme.Border AddBorderTheme(string name, Texture2D bordertopleft, Texture2D bordertopright, Texture2D borderbottomleft, Texture2D borderbottomright,
							Texture2D borderleft, Texture2D borderright, Texture2D bordertop, Texture2D borderbottom)
		{
			if (ThemeNameIsFree(name))
			{
				Theme.Border newBorderTheme = new Theme.Border(name, bordertopleft, bordertopright, borderbottomleft, borderbottomright, borderleft, borderright, bordertop, borderbottom);
				themes.Add(newBorderTheme);
				return newBorderTheme;
			}
			else
			{
				throw new NameException("Theme name already exists.");
			}
		}

		public Theme.Button AddButtonTheme(string name, Texture2D texture, SpriteFont font, string text = "")
		{
			if (ThemeNameIsFree(name))
			{
				Theme.Button newButtonTheme = new Theme.Button(name, texture, font, text);
				themes.Add(newButtonTheme);
				return newButtonTheme;
			}
			else
			{
				throw new NameException("Theme name already exists.");
			}
		}

		public Theme.Dialogue AddDialogueTheme(string name, Theme.Border borderTheme, Theme.Button buttonTheme, SpriteFont font, Texture2D background)
		{
			if (ThemeNameIsFree(name))
			{
				Theme.Dialogue newDialogueTheme = new Theme.Dialogue(name, borderTheme, buttonTheme, font, background);
				themes.Add(newDialogueTheme);
				return newDialogueTheme;
			}
			else
			{
				throw new NameException("Theme name already exists.");
			}
		}

		public Theme.Menu AddMenuTheme(string name, SpriteFont font, Texture2D selectionBox)
		{
			if (ThemeNameIsFree(name))
			{
				Theme.Menu newMenuTheme = new Theme.Menu(name, font, selectionBox);
				themes.Add(newMenuTheme);
				return newMenuTheme;
			}
			else
			{
				throw new NameException("Theme name already exists.");
			}
		}

		public Theme.Pointer AddPointerTheme(string name, Texture2D pointerTop, Texture2D pointerBody, Texture2D pointerBottom)
		{
			if (ThemeNameIsFree(name))
			{
				Theme.Pointer newPointerTheme = new Theme.Pointer(name, pointerTop, pointerBody, pointerBottom);
				themes.Add(newPointerTheme);
				return newPointerTheme;
			}
			else
			{
				throw new NameException("Theme name already exists.");
			}
		}

		public Theme.TextBox AddTextBoxTheme(string name, Theme.Border borderThemes, Theme.Pointer pointerTheme, Texture2D background, SpriteFont textFont)
		{
			if (ThemeNameIsFree(name))
			{
				Theme.TextBox newTextBoxTheme = new Theme.TextBox(name, borderThemes, pointerTheme, background, textFont);
				themes.Add(newTextBoxTheme);
				return newTextBoxTheme;
			}
			else
			{
				throw new NameException("Theme name already exists.");
			}
		}

		public Theme.WindowVar AddWindowVarTheme(string name, SpriteFont titleFont, Theme.Border headerBorder, Texture2D headerBackground, Theme.Border windowBorder, Texture2D background)
		{
			if (ThemeNameIsFree(name))
			{
				Theme.WindowVar newWindowVarTheme = new Theme.WindowVar(name, titleFont, headerBorder, headerBackground, windowBorder, background);
				themes.Add(newWindowVarTheme);
				return newWindowVarTheme;
			}
			else
			{
				throw new NameException("Theme name already exists.");
			}
		}

		internal Theme.Theme GetTheme(string themeName)
		{
			foreach (Theme.Theme theme in themes)
			{
				if (theme.Name == themeName)
				{
					return theme;
				}
			}
			return null;
		}

		private bool ThemeNameIsFree(string name)
		{
			foreach (Theme.Theme theme in themes)
			{
				if (theme.Name == name)
				{
					return false;
				}
			}
			return true;
		}
	}
}
